package com.yu.work.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.yu.work.R;
import com.yu.work.domain.Record;
import com.yu.work.service.RecordService;
import com.yu.work.thread.DrawNumberThread;
import com.yu.work.util.DrawNumberSwitchUtils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.Random;

/**
 * 抽号页面
 */
@ContentView(R.layout.fragment_draw_number)
public class DrawNumberFragment extends Fragment {

    private static final int FLASH_TARGET_NUMBER = 1;

    @ViewInject(R.id.tv_target_number)
    private TextView tv_targetNumber;   //用于展示随机抽号的文本框

    @ViewInject(R.id.edt_max_number)
    private EditText edt_maxNumber;     //输入的最大人数

    @ViewInject(R.id.fab_save)
    private FloatingActionButton fab_save_record;     //保存按钮

    //用于标识抽号是否处于运行状态
    //运行状态为true，停止状态为false
    private boolean flag = false;
    private DrawNumberThread drawNumberThread;
    private RecordService mRecordService = RecordService.getInstance();

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // FLASH_TARGET_NUMBER 代表是刷新数字的消息
            if (msg.what == FLASH_TARGET_NUMBER) {
                tv_targetNumber.setText(msg.obj.toString());
            }
        }
    };


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             Bundle savedInstanceState) {
        View v = x.view().inject(this, inflater, container);
        return v;
    }

    /**
     * 抽号开关的点击事件
     *
     * @param v
     */
    @Event(R.id.btn_draw_number_switch)
    private void drawNumber(View v) {
        String text = edt_maxNumber.getText().toString();
        if ("".equals(text) || "0".equals(text)) {
            Snackbar.make(v, "请输入正确人数", Snackbar.LENGTH_SHORT).show();
            return;
        }

        //改变按钮样式
        flag = DrawNumberSwitchUtils.toggle((Button) v);

        if (flag) {
            drawNumberThread = new DrawNumberThread(handler);
            int max = Integer.valueOf(edt_maxNumber.getText().toString());
            drawNumberThread.setMaxNumber(max);
            //开启线程
            drawNumberThread.start();
        } else {
            drawNumberThread.setFlag(false);
        }

        //抽号状态下不可编辑
        edt_maxNumber.setEnabled(!flag);
    }

    @Event(R.id.fab_save)
    private void saveRecord(View v) {
        if (flag) {//如果处于抽号中
            Snackbar.make(v, "抽号中不可保存", Snackbar.LENGTH_SHORT).show();
            return;
        }

        try {
            final Long targetNumber = Long.valueOf(tv_targetNumber.getText().toString());
            //检查该号码是否保存过
            boolean unique = mRecordService.isUnique(targetNumber);
            if (!unique) {//如果保存过
                Snackbar.make(v, "该号码已经存在，是否继续保存", Snackbar.LENGTH_LONG)
                        .setAction("保存", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Record record = new Record();
                                record.setTargetNumber(targetNumber);
                                mRecordService.save(record);
                                Toast.makeText(getActivity(), "保存成功", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .show();
            } else {//没有保存过
                Record record = new Record();
                record.setTargetNumber(targetNumber);
                mRecordService.save(record);
                Snackbar.make(v, "保存成功", Snackbar.LENGTH_SHORT).show();
            }
        } catch (NumberFormatException e) {
            Snackbar.make(v, "尚未抽号", Snackbar.LENGTH_SHORT).show();
        }
    }

}
